package com.hopu.v1.controller.admin.system;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.common.Save;
import com.hopu.v1.pojo.common.Update;
import com.hopu.v1.pojo.exam.ExamRecords;
import com.hopu.v1.pojo.system.Announcement;
import com.hopu.v1.service.admin.system.AnnouncementService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Announcement)表控制层
 *
 * @author makejava
 * @since 2024-10-10 17:07:20
 */
@RestController
@RequestMapping("announcement")
public class AnnouncementController {
    /**
     * 服务对象
     */
    @Resource
    private AnnouncementService announcementService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public R selectAll(String announcementTitle,Integer announcementStatus) {
        return R.ok(this.announcementService.selectAnnouncement(announcementTitle,announcementStatus));
    }


    /**
     * 新增数据
     *
     * @param announcement 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public R insert(@Validated(Save.class) Announcement announcement) {
        this.announcementService.insertAnnouncement(announcement);
        return R.ok("添加成功");
    }

    /**
     * 修改数据
     *
     * @param announcement 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public R update(@Validated(Update.class) Announcement announcement) {
        this.announcementService.updateAnnouncement(announcement);
        return R.ok("修改成功");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @GetMapping("/delete/{ids}")
    public R delete(@PathVariable List<Integer> ids) {
        this.announcementService.deleteAnnouncement(ids);
        return R.ok("删除成功");
    }

    /**
     * 修改状态创建发布时间
     * @param announcement
     * @return
     */
    @PostMapping("updateStatus")
    public R updateStatus(Announcement announcement) {
        announcementService.updateStatus(announcement);
        return R.ok("修改状态成功！");
    }

    /**
     * 获取发布的公告
     * @param page
     * @param announcementStatus
     * @return
     */
    @GetMapping("select")
    public R select(Page<Announcement> page, Integer announcementStatus) {
        LambdaQueryWrapper<Announcement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Announcement::getAnnouncementStatus, announcementStatus);
        wrapper.orderByDesc(Announcement::getAnnouncementReleased);
        return R.ok(announcementService.page(page,wrapper));
    }
}

